home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / general / modelers / geomview / source.lha / Geomview / src / lib / geometry / transobj / transobj.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-01-29  |  3.6 KB  |  175 lines

  1. /* Copyright (c) 1992 The Geometry Center; University of Minnesota
  2.    1300 South Second Street;  Minneapolis, MN  55454, USA;
  3.    
  4. This file is part of geomview/OOGL. geomview/OOGL is free software;
  5. you can redistribute it and/or modify it only under the terms given in
  6. the file COPYING, which you should have received along with this file.
  7. This and other related software may be obtained via anonymous ftp from
  8. geom.umn.edu; email: software@geom.umn.edu. */
  9.  
  10. /* Authors: Charlie Gunn, Pat Hanrahan, Stuart Levy, Tamara Munzner, Mark Phillips */
  11.  
  12. #include "transform.h"
  13. #include "transobj.h"
  14. #include "handleP.h"
  15.  
  16. /* 
  17.  * Operations on TransObj objects.
  18.  */
  19.  
  20. void
  21. TransPosition(TransObj *tobj, Transform T)    /* Get transform into T */
  22. { TmCopy(tobj->T, T); }
  23.  
  24. void
  25. TransTransformTo(TransObj *tobj, Transform T)    /* Set transform from T */
  26. { TmCopy(T, tobj->T); }
  27.  
  28. void
  29. TransDelete(TransObj *tobj)
  30. {
  31.     if(tobj != NULL && RefDecr((Ref *)tobj) <= 0)
  32.     OOGLFree(tobj);
  33. }
  34.  
  35. TransObj *
  36. TransCreate( Transform T )
  37. {
  38.     TransObj *tobj = OOGLNewE(TransObj, "TransObj");
  39.     RefInit((Ref*)tobj, TRANSMAGIC);
  40.     if(T != TMNULL) TmCopy(T, tobj->T);
  41.     return tobj;
  42. }
  43.  
  44.  
  45. /*
  46.  * Communications
  47.  */
  48.  
  49. HandleOps TransOps = {
  50.     "trans",
  51.     TransStreamIn,
  52.     TransStreamOut,
  53.     TransDelete,
  54.     NULL,
  55.     NULL
  56. };
  57.  
  58. void
  59. TransUpdate( Handle **hp, Ref *ignored, Transform Tfixme )
  60. {
  61.     register Handle *h = *hp;
  62.  
  63.     if(h != NULL && h->object != NULL)
  64.     TmCopy( ((TransObj *)(h->object))->T, Tfixme );
  65. }
  66.  
  67.  
  68.  
  69. TransStreamIn( Pool *p, Handle **hp, Transform T )
  70. {
  71.     Handle *h = NULL;
  72.     Handle *hname = NULL;
  73.     register TransObj *tobj = NULL;
  74.     char *w, *raww;
  75.     int c;
  76.     int more = 0;
  77.     int brack = 0;
  78.     FILE *inf;
  79.  
  80.     if(p == NULL || (inf = PoolInputFile(p)) == NULL)
  81.     return 0;
  82.  
  83.  
  84.     do {
  85.     more = 0;
  86.     switch(c = fnextc(inf, 0)) {
  87.     case '{': brack++; fgetc(inf); break;
  88.     case '}': if(brack--) fgetc(inf); break;
  89.     case 't':
  90.         if(fexpectstr(inf, "transform"))
  91.         return 0;
  92.         more = 1;
  93.         break;
  94.  
  95.     case 'd':
  96.         if(fexpectstr(inf, "define"))
  97.         return 0;
  98.         hname = HandleAssign(ftoken(inf, 0), &TransOps, NULL);
  99.         break;
  100.  
  101.     case '<':
  102.     case ':':
  103.     case '@':
  104.         fgetc(inf);
  105.         w = fdelimtok("{}()", inf, 0);
  106.         if(c == '<' && HandleByName(w, &TransOps) == NULL) {
  107.         w = findfile(PoolName(p), raww = w);
  108.         if(w == NULL) {
  109.             OOGLSyntax(inf, "Reading transform from \"%s\": can't find file \"%s\"",
  110.             PoolName(p), raww);
  111.         }
  112.         }
  113.         h = HandleReferringTo(c, w, &TransOps, NULL);
  114.         if(h)
  115.         tobj = (TransObj *)h->object;
  116.         break;
  117.  
  118.     default:
  119.         /*
  120.          * Anything else should be a 4x4 matrix
  121.          */
  122.         if(tobj == NULL)
  123.         tobj = TransCreate( TMNULL );
  124.         if(fgettransform(inf, 1, &tobj->T[0][0], 0) <= 0)
  125.         return 0;
  126.     }
  127.     } while(brack || more);
  128.  
  129.     if(hname != NULL) {
  130.     if(tobj)
  131.         HandleSetObject(hname, (Ref *)tobj);
  132.     h = hname;
  133.     }
  134.  
  135.     if(h == NULL && tobj != NULL && p->ops == &TransOps)
  136.     h = HandleAssign(PoolName(p), &TransOps, (Ref *)tobj);
  137.  
  138.  
  139.     if(h != NULL && tobj != NULL)
  140.     HandleSetObject(h, (Ref *)tobj);
  141.  
  142.     if(tobj != NULL && T != TMNULL)
  143.     TmCopy(tobj->T, T);
  144.  
  145.     if(h != NULL && hp != NULL) {
  146.     if(*hp != h) {
  147.         if(*hp != NULL)
  148.         HandlePDelete(hp);
  149.         *hp = h;
  150.     }
  151.     } else {
  152.     TransDelete(tobj);    /* Maintain ref count */
  153.     }
  154.  
  155.     return (h != NULL || tobj != NULL);
  156. }
  157.  
  158. int
  159. TransStreamOut( Pool *p, Handle *h, Transform T )
  160. {
  161.     int putdata;
  162.     FILE *outf;
  163.  
  164.     if((outf = PoolOutputFile(p)) == NULL)
  165.     return 0;
  166.  
  167.     fprintf(outf, "transform {\n");
  168.  
  169.     putdata = PoolStreamOutHandle( p, h, T != TMNULL );
  170.     if(putdata)
  171.     fputtransform(outf, 1, &T[0][0], 0);
  172.     fputs("}\n", outf);
  173.     return !ferror(outf);
  174. }
  175.